package 剑指Offer.easy;

public class 反转链表 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode front = null;
            ListNode behind = null;
            while (head != null) {
                //保留后续结点
                behind = head.next;
                //反转当前结点指向
                head.next = front;
                //更新前面结点
                front = head;
                //更新当前结点
                head = behind;
            }
            return front;
        }
    }
}
